package hong.jun.pet_home.domain.util;

import hong.jun.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

@Service
public class SendEmailUtil {

    @Autowired
    private JavaMailSender mailSender;

    //发邮件
    public  AjaxResult sendEmail(EmailMessage emailMessage) {
        try {
            if (emailMessage == null){
                return new AjaxResult("邮件创建失败");
            }
            //创建复杂邮件对象
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("1737619387@qq.com");
            helper.setSubject(emailMessage.getTitle());//标题
            helper.setText(emailMessage.getTheme()//主体
                    , true);
            helper.setTo(emailMessage.getEmailtouser());//收件email
            mailSender.send(mimeMessage);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(emailMessage.getReturnmessage());
        }
    }
}
